// Created by lifei on 2020/7/20--18:30.
/*import baseInfo from './components/base-info'
import matchInfo from './components/match-info'
import dochInfo from './components/doc-info'

export default {
    name: 'resource-manage-edit',
    components: {baseInfo, matchInfo, dochInfo},
    data() {
        return {
            tableData: []
        }
    },
    methods: {
        selectionChange() {

        }
    }
}*/
import {milestonetemplate, getMilestonetemplate, milestonetemplateEdit} from '../api/docSetting'
import {updatePayableAdjustmentByCommit} from "../../../../wy-lessor/finance/api/lessor-payable";
import el from "element-ui/src/locale/lang/el";
// hxf-0829-修改项目档案设置编辑与新增
export default {
    name: 'doc-edit',
    data() {
        return {
            // hxf-0829-排序号多选暂时写死
            options: [
                {
                    value: '1',
                    label: 1
                },
                {
                    value: '2',
                    label: 2
                },
                {
                    value: '3',
                    label: 3
                },
                {
                    value: '4',
                    label: 4
                }
            ],
            // hxf-0907-附件信息是否必填
            RequiredOptions: [
                {
                    value: '1',
                    label: '是'
                },
                {
                    value: '2',
                    label: '否'
                }
            ],
            // hxf-0907-新增标识
            isAdd: true,
            // hxf-0908-弹框默认为false
            dialogVisible: false,
            // hxf-0908-弹框资料
            bulletInformation: {
                milepostName: '',
                remarks: '',
                sortNumber: '1'
            },
            // hxf-0908-基本信息校验
            formSettingRules: {
                archivesName: [
                    { required: true, message: "请输入模板名称", trigger: "blur" },
                    { min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur" }
                ],
            },
            // hxf-0908-弹框资料校验
            bulletInformationRules: {
                milepostName: [
                    { required: true, message: "请输入里程碑名称", trigger: "blur" },
                    { min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur" }
                ]/*,
                sortNumber: [
                    { required: true, message: "请输入里程碑排序号", trigger: "blur" },
                    { min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur" }
                ]*/
            },
            // hxf-0909-提交form表单
            projectDocForm: {
                // hxf-0829-基本信息
                milestoneTemplate: {
                    archivesName: '',
                    remarks: ''
                },
                // hxf-0909-里程碑表格
                milestoneConfigVoList: [],
            },
            // hxf-0909-存入id
            archivesId: '',
            // hxf-0909-里程碑表格
            editableTabsValue: '0',
            tabIndex: 0
        }
    },
    methods: {
        // hxf-0907-附件信息编辑
        handleClickEdit(row) {
            this.$set(row, 'newAdd', true)
            this.$forceUpdate()
        },
        // hxf-0907-附件信息删除
        handleClickDelete(index, rows) {
            rows.splice(index, 1);

            //删除后重新排序
            rows = rows.map((r,index) => { r.sort = index+1})
        },
        // hxf-0829-更新
        handleUpdate() {
            this.$refs['doc-setting'].validate((valid) => {
                if (valid) {
                    this.projectDocForm.milestoneConfigVoList.map((item,index) => {
                        this.projectDocForm.milestoneConfigVoList[index].milestoneName = item.title
                        this.projectDocForm.milestoneConfigVoList[index].milestoneMaterials = item.content
                        this.projectDocForm.milestoneConfigVoList[index].milestoneSort = index+1
                    })
                    if(this.projectDocForm.milestoneConfigVoList.length > 0) {

                        let errFlag = false;
                        this.projectDocForm.milestoneConfigVoList.map((item, index) => {
                            if (item.milestoneMaterials.length <= 0 || (item.milestoneMaterials[index] && item.milestoneMaterials[index].materialsName === '')) {
                                errFlag = true
                            }
                        })
                        if(errFlag){
                            this.$message.error('请完善里程碑信息！')
                            return ;
                        }
                        milestonetemplateEdit(this.projectDocForm).then(res => {
                            if (res.data.code === 0) {
                                this.closeNowRouter();
                                this.$message.success('更新成功！')
                            }
                        })
                    } else {
                        milestonetemplateEdit(this.projectDocForm).then(res => {
                            if (res.data.code === 0) {
                                this.closeNowRouter();
                                this.$message.success('更新成功！')
                            }
                        })
                    }
                } else {
                    this.$message.error('请完善必填信息！')
                    return false;
                }
            })
        },
        // hxf-0829-保存
        handlePreservation() {
            this.$refs['doc-setting'].validate((valid) => {
                if (valid) {
                    this.projectDocForm.milestoneConfigVoList.map((item,index) => {
                        this.projectDocForm.milestoneConfigVoList[index].milestoneName = item.title
                        this.projectDocForm.milestoneConfigVoList[index].milestoneMaterials = item.content
                        this.projectDocForm.milestoneConfigVoList[index].milestoneSort = index+1
                    })
                    if(this.projectDocForm.milestoneConfigVoList.length > 0) {
                        let errFlag = false;
                        this.projectDocForm.milestoneConfigVoList.map((item,index) => {
                            if (item.milestoneMaterials.length <= 0 || item.milestoneMaterials[index].materialsName === '') {
                                errFlag = true;
                            }
                        })
                        if(errFlag){
                            this.$message.error('请完善里程碑信息！')
                            return ;
                        }
                        milestonetemplate(this.projectDocForm).then(res => {
                            if (res.data.code === 0) {
                                this.closeNowRouter();
                                this.$message.success('添加成功！')
                            }
                        })
                    } else {
                        milestonetemplate(this.projectDocForm).then(res => {
                            if (res.data.code === 0) {
                                this.closeNowRouter();
                                this.$message.success('添加成功！')
                            }
                        })
                    }
                } else {
                    this.$message.error('请完善必填信息！')
                    return false;
                }
            })
        },
        // hxf-0907-附件添加
        superInforAdd() {
            this.dialogVisible = true
            //hxf-若为编辑，则排序号不重新拍
            if (this.projectDocForm.milestoneConfigVoList.length > 0) {
                this.bulletInformation.sortNumber = this.projectDocForm.milestoneConfigVoList.length + 1
            }
        },
        // hxf-0908-X弹框关闭
        beforeClose(done) {
            done()
        },
        // hxf-0908-弹框信息确认
        handleDialogClick(targetName) {
            this.$refs['dialog-form'].validate((valid) => {
                if (valid) {
                    let newTabName = ++this.tabIndex + '';
                    this.projectDocForm.milestoneConfigVoList.push({
                        title: this.bulletInformation.milepostName,
                        name: newTabName,
                        sortNumber: this.bulletInformation.sortNumber,
                        content: []
                    });
                    this.editableTabsValue = newTabName;
                    this.dialogVisible = false
                    this.$nextTick(function () {
                        // this.bulletInformation = {}
                        this.bulletInformation.milepostName = ''
                        this.bulletInformation.remarks = ''
                    })
                    this.$message.success('添加成功！')
                    this.bulletInformation.sortNumber = Number(this.bulletInformation.sortNumber) + 1
                } else {
                    this.$message.error('请完善必填信息！')
                    return false;
                }
            })
        },
        // hxf-0908-删除弹框tabs
        removeTab(targetName) {
            this.$confirm('是否确认删除里程碑?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let tabs = this.projectDocForm.milestoneConfigVoList;
                let activeName = this.editableTabsValue;
                if (activeName === targetName) {
                    tabs.forEach((tab, index) => {
                        if (tab.name === targetName) {
                            let nextTab = tabs[index + 1] || tabs[index - 1];
                            if (nextTab) {
                                activeName = nextTab.name;
                            }
                        }
                    });
                }
                this.editableTabsValue = activeName;
                this.projectDocForm.milestoneConfigVoList = tabs.filter(tab => tab.name !== targetName);

                this.$message.success('删除成功！')
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        // hxf-0908-里程碑详细信息添加
        milestoneDetailsAdd(index) {
            this.projectDocForm.milestoneConfigVoList[index].content.push({
                archivesId: '',
                materialsName: '',
                isReuse: '0',
                isRequired: '0',
                // sort: this.projectDocForm.milestoneConfigVoList[index].sortNumber,
                sort: this.projectDocForm.milestoneConfigVoList[index].content.length + 1,
                newAdd: true,
            })
            if (this.projectDocForm.milestoneConfigVoList[index].content.length === 1) {
                this.projectDocForm.milestoneConfigVoList[index].content[0].isReuse = '0'
            }
        },
        closeNowRouter() {
            //关闭当前新增页，暂时没找到好办法
            this.$store.commit("delHistoryRoute", this.$route.fullPath);
            // this.$parent.$children[2].closeTab('current')
            //跳转
            this.$router.push("/property-plan-manage/project-doc-setting/list");
        },
        // hxf-0909-编辑时初始获取初始数据
        handleGetMilestonetemplate(id) {
            if (id !== '' && id !== null && id !== undefined) {
                getMilestonetemplate(id).then(res => {
                    if (res.data.code === 0) {
                        this.projectDocForm = res.data.data
                        this.projectDocForm.milestoneConfigVoList.map((item,index) => {
                            this.projectDocForm.milestoneConfigVoList[index].title = item.milestoneName
                            this.projectDocForm.milestoneConfigVoList[index].content = item.milestoneMaterials
                            this.projectDocForm.milestoneConfigVoList[index].name = index.toString()
                            this.tabIndex = index.toString()
                            // this.projectDocForm.milestoneConfigVoList[index].content[0].isReuse = '0'
                        })
                    }
                })
            }
        },
        /*//hxf-0924-函数防抖
        debounce(func, wait) {
            let timeout;
            return function () {
                let context = this;
                let args = arguments;
                if (timeout) clearTimeout(timeout);
                timeout = setTimeout(() => {
                    func.apply(context, args)
                }, wait);
            }
        },
        //hxf-0924-判断滚动方向
        handleScroll(e){
            let direction = e.deltaY > 0 ? 'down' : 'up'
            this.options.map(item => {
                if(item.label <= 1){
                    direction == 'down' ? item.label++ : item.label = 1
                    item.value = item.label + ''
                } else {
                    direction == 'down' ? item.label++ : item.label--
                    item.value = item.label + ''
                }
            })
        }*/
    },
    created() {
        this.archivesId = this.$route.query.id
        if (this.$route.meta.title !== '新增') {
            this.isAdd = false
        }
    },
    mounted() {
        this.handleGetMilestonetemplate(this.archivesId)
       /* window.addEventListener('mousewheel', this.debounce(this.handleScroll,300), true)||window.addEventListener("DOMMouseScroll",this.debounce(this.handleScroll,300),false)*/
    }
}
